import 'package:flutter/material.dart';
import 'package:flutter_ncsjqz_getx/page/rong_im/commom/address_book/address_list_logic.dart';
import 'package:flutter_ncsjqz_getx/page/rong_im/commom/address_book/address_list_view.dart';
import 'package:flutter_ncsjqz_getx/widget/gradient_button.dart';
import 'package:flutter_switch/flutter_switch.dart';
import 'package:get/get.dart';

import '../../util/rong_widget.dart';
import 'isolate_logic.dart';

/// <summary>
/// author：zwb
/// dateTime： 2021/12/15 17:59
/// filePath： lib/page/rong_im/isolate_view.dart
/// desc: 自我隔离
/// <summary>
///
class IsolatePage extends StatefulWidget {

  @override
  _IsolatePageState createState() => _IsolatePageState();
}

class _IsolatePageState extends State<IsolatePage> {
  final logic = Get.put(IsolateLogic());

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    logic.getData();
  }
  @override
  Widget build(BuildContext context) {
    return GetBuilder<IsolateLogic>(
      builder: (controller) {
        return Scaffold(
          appBar: RongWidget.appBarWidget(title: "自我隔离",actions: [
            Padding(
              padding: EdgeInsets.only(right: 15),
              child: RongWidget.imgAsset("setting1.png",onTap: (){

              },width: 20),
            )
          ],),
          body: ListView(
            padding: EdgeInsets.all(0),
            children: [
              RongWidget.rowBaseWidget(
                  padding: EdgeInsets.all(15),
                  leftText: "是否开启自我隔离模式",
                  right: FlutterSwitch(
                    height: 25,
                    width: 50,
                    toggleSize: 22.0,
                    padding: 0,
                    value: logic.showSetting,
                    activeColor: Colors.green,
                    onToggle: (val) => logic.setShowSetting(val),
                  ),
                  onTap: (){

                  }
              ),
              SizedBox(height: 10,),
              Offstage(
                child: Column(
                  children: [
                    RongWidget.rowBaseWidget(
                        padding: EdgeInsets.all(15),
                        leftText: "开启隔离时间",
                        rightText: "2021-03-05 15:22:11",
                        onTap: (){
                          RongWidget.openDateTime(
                            context,
                            initialDateTime: logic.startDate,
                            minDateTime: logic.startDate, // 最小开始日期 今天
                            maxDateTime: DateTime(DateTime.now().year + 50),
                            onConfirm: (value) => logic.setStartDate(value),
                          );
                        }
                    ),
                    RongWidget.rowBaseWidget(
                        padding: EdgeInsets.all(15),
                        leftText: "结束隔离时间",
                        rightText: "2021-05-05 15:22:11",
                        onTap: (){
                          RongWidget.openDateTime(
                            context,
                            initialDateTime: logic.endDate,
                            minDateTime: logic.startDate,// 最小结束日期 今天
                            maxDateTime: DateTime(DateTime.now().year + 50),
                            onConfirm: (value) => logic.setEndDate(value),
                          );
                        }
                    ),
                    RongWidget.rowBaseWidget(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      padding: EdgeInsets.all(15),
                      leftText: "隔离范围",
                      maxLines: 99,
                      rightFlex: 3,
                      rightText: "${logic.glName}",
                      onTap: () {
                        print("隔离范围");
                        Get.to(()=>AddressListPage(type: 2,title: "隔离范围",))!.then((value) {
                          if(value!=null){
                            List<ContactInfo?> users = value;
                            logic.glName = "";
                            logic.ryUserIds = [];
                            logic.blockUserIds = [];
                            users.forEach((element){
                              logic.ryUserIds.add(element!.ryUserId);
                              logic.blockUserIds.add(element.friendId);
                              logic.glName+=element.name+"、";
                            });
                            logic.glName = logic.glName.substring(0, logic.glName.lastIndexOf('、'));
                            print("选择了：$value");
                            logic.update();
                          }
                        });
                      },
                    ),

                    SizedBox(height: 20,),

                    SizedBox(
                      child: gradientButton("提交隔离设置",onTap: (){
                        logic.setData();
                      }),
                    ),
                  ],
                ),
                offstage: !logic.showSetting,
              ),
            ],
          ),
        );
      },
    );
  }

  @override
  void dispose() {
    Get.delete<IsolateLogic>();
    super.dispose();
  }
}